package com.note.simpleshop.module.goods.service;
import com.note.simpleshop.entity.TbGoodsSpec;
import com.note.simpleshop.module.goods.dao.GoodsSpecRepository;
import com.note.simpleshop.module.goods.vo.GoodsSpecVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class GoodsSpecService {
    private static final String CACHE_NAME="goods_spec_cache";
    @Autowired
    private GoodsSpecRepository goodsSpecRepository;
    @Cacheable(CACHE_NAME)
    public List<GoodsSpecVo> findByGoodsId(String goodsId)
    {
        List<GoodsSpecVo> listVo=new ArrayList<GoodsSpecVo>();
        List<TbGoodsSpec> list= goodsSpecRepository.findByGoodsId(goodsId);
        for(TbGoodsSpec goodsSpec:list)
        {
            GoodsSpecVo vo=GoodsSpecVo.newInstance(goodsSpec);
            listVo.add(vo);
        }
        return listVo;
    }
    public TbGoodsSpec findById(String id)
    {
        return goodsSpecRepository.getOne(id);
    }
    @Cacheable(CACHE_NAME)
    public TbGoodsSpec findCacheById(String id)
    {
        return findById(id);
    }
    public void updateStock(Integer num, String id)
    {
        goodsSpecRepository.updateStock(num,id);
    }
}
